<?php
namespace app\common\model;

use app\extra\BaseModel;
use app\extra\DBCont;

class Team extends BaseModel
{
    protected $auto = [];
    protected $insert = ['share_id'];
    public function setShareIdAttr($value, $data){
        if (empty($value) && !empty($data['code'])){
           $value = model('BagMan')->where('code', '=', $data['code'])->value('id');
           if (empty($value)){
               $value = null;
           }
        }
        return $value;
    }
    public function getCoverAttr($value, $data){
        if (empty($value)){
            $value = model('Member')->where('team_id', $data['id'])->value('image');
            if (empty($value)){
                return null;
            }
        }
        return [
            'key' => $value,
            'value' => showFileUrl($value)
        ];
    }
    public function setCoverAttr($value){
        return serializeByArray($value, ',');
    }
    public function getMatchIdAttr($value){
        if (empty($value)){
            return null;
        }
        return [
            'key' => $value,
            'value' => model('Match')->where('id', $value)->value('name')
        ];
    }
    public function getCateIdAttr($value){
        if (empty($value)){
            return null;
        }
        return [
            'key' => $value,
            'value' => model('MatchCate')->where('id', $value)->value('name')
        ];
    }
    /**
     * 添加额外参数
     * @param $item
     */
    public function setAddedField(&$item){
        $where = [
            ['team_id', '=', $item['id']],
//            ['pay_state', '=', DBCont::SUCCESS],
//            ['status', '=', DBCont::SUCCESS],
        ];
        $item['member_count'] = model('Member')->where($where)->count();
        if ($item['type'] == DBCont::PERSON){
            $item['member_id'] = model('Member')->where($where)->value('id');
        } else {
            $item['member_id'] = model('Member')->where($where)->column('id');
        }
    }
    /**
     * 新增团队赛
     * @param $data
     */
    public function addTeam($data){
        $data['type'] = DBCont::TEAM;
        try {
            $this->startTrans();
            $validate = Validate('Team');
            if (!$validate->check($data)) {
                \exception($validate->getError());
            }
            $r = $this->createAdd($data);
            if ($r === false){
                \exception($this->getError());
            }
            $r = model('Member')->addMember($data, $r['id']);
            if ($r === false){
                \exception(model('Member')->getError());
            }
            $this->commit();
            return $r;
        } catch (\Exception $e){
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
    public function createAdd($data = [], $field = null, $replace = false)
    {
        $where = [
            ['id', '=', $data['match_id']],
            ['status', '=', DBCont::SUCCESS],
            ['start_time', '<', time()],
            ['end_time', '>', time()],
        ];
        if (empty(model('Match')->where($where)->value('id'))){
            $this->error = '该赛事或活动是非进行中，请联系管理员:team';
            return false;
        }
        if ($data['type'] == DBCont::TEAM){
            $id = $this->where('title', $data['title'])
                ->where('match_id', $data['match_id'])
                ->where('type', DBCont::TEAM)
                ->where('status', DBCont::SUCCESS)
                ->value('id');
            if ($id){
                $this->error = '团队名称已经存在，请更换团队名称';
                return false;
            }
            $id = $this->where('title', $data['title'])
                ->where('match_id', $data['match_id'])
                ->where('type', DBCont::TEAM)
                ->where('create_time', '>', time()-1*60*60)
                ->where('status', DBCont::WAIT)
                ->value('id');
            if ($id){
                $this->error = '该团队名称被占用，请稍后再试';
                return false;
            }
        }
        return parent::createAdd($data, $field, $replace);
    }
}